class LuckyWheel {
    constructor() {
        this.canvas = document.getElementById('wheel');
        this.ctx = this.canvas.getContext('2d');
        this.startBtn = document.getElementById('startBtn');
        this.stopBtn = document.getElementById('stopBtn');
        
        this.options = [
            "加5积分",
            "再抽一次",
            "自由换位置一天",
            "免作业一天",
            "老师手工表扬",
            "下课提前5分钟",
            "免值日一周",
            "与老师共进午餐",
            "全班鼓掌",
            "当一天老师小助手"
        ];
        
        this.currentAngle = 0;
        this.isSpinning = false;
        this.spinSpeed = 0;
        
        this.init();
    }
    
    init() {
        this.startBtn.addEventListener('click', () => this.startSpin());
        this.stopBtn.addEventListener('click', () => this.stopSpin());
        this.draw();
    }
    
    draw() {
        const ctx = this.ctx;
        const center = this.canvas.width / 2;
        const radius = this.canvas.width / 2 - 10;
        
        // 清空画布
        ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
        
        // 绘制扇形
        this.options.forEach((option, i) => {
            const startAngle = (i * 2 * Math.PI / this.options.length) + this.currentAngle;
            const endAngle = ((i + 1) * 2 * Math.PI / this.options.length) + this.currentAngle;
            
            ctx.beginPath();
            ctx.moveTo(center, center);
            ctx.arc(center, center, radius, startAngle, endAngle);
            ctx.fillStyle = i % 2 ? '#FFB6C1' : '#87CEEB';
            ctx.fill();
            ctx.closePath();
            
            // 绘制文字
            ctx.save();
            ctx.translate(center, center);
            ctx.rotate(startAngle + Math.PI / this.options.length);
            ctx.textAlign = 'right';
            ctx.fillStyle = '#000';
            ctx.font = '14px Arial';
            ctx.fillText(option, radius - 20, 0);
            ctx.restore();
        });
        
        // 绘制中心点和指针
        ctx.beginPath();
        ctx.moveTo(center, 10);
        ctx.lineTo(center - 10, 30);
        ctx.lineTo(center + 10, 30);
        ctx.fillStyle = 'red';
        ctx.fill();
        
        ctx.beginPath();
        ctx.arc(center, center, 10, 0, 2 * Math.PI);
        ctx.fillStyle = 'red';
        ctx.fill();
    }
    
    startSpin() {
        this.isSpinning = true;
        this.spinSpeed = 0.2 + Math.random() * 0.2;
        this.startBtn.disabled = true;
        this.stopBtn.disabled = false;
        this.animate();
    }
    
    stopSpin() {
        this.isSpinning = false;
        this.stopBtn.disabled = true;
    }
    
    animate() {
        if (this.isSpinning || this.spinSpeed > 0.001) {
            this.currentAngle += this.spinSpeed;
            if (!this.isSpinning) {
                this.spinSpeed *= 0.95;
            }
            this.draw();
            requestAnimationFrame(() => this.animate());
        } else {
            this.startBtn.disabled = false;
        }
    }
}

// 创建转盘实例
const wheel = new LuckyWheel(); 